//
//  ExMainViewController.m
//  OC-Example
//
//  Created by apang on 2018/8/8.
//  Copyright © 2018 apang.men All rights reserved.
//

#import "ExMainViewController.h"

@interface ExMainViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic , copy) NSArray * viewControllers;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end

@implementation ExMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setEdgesForExtendedLayout:UIRectEdgeNone];
    if (@available(iOS 11.0, *)) {
        _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    NSString * filePath = [[NSBundle mainBundle] pathForResource:@"viewControllers" ofType:@"json"];
    NSData * fileData = [NSData dataWithContentsOfFile:filePath];
    _viewControllers = [NSJSONSerialization JSONObjectWithData:fileData options:NSJSONReadingMutableLeaves error:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark UITableView's Delegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return _viewControllers.count;
}
- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return ((NSArray *)[_viewControllers[section] objectForKey:@"items"]).count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"example"];
    
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"example"];
    }
    
    NSDictionary * sections = _viewControllers[indexPath.section];
    
    NSArray * items = [sections objectForKey:@"items"];
    
    NSDictionary * item = items[indexPath.row];
    
    cell.textLabel.text = [item objectForKey:@"title"];
    
    cell.detailTextLabel.text = [item objectForKey:@"class"];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 40;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_BOUNDS.width, 40)];
    //    [header setBackgroundColor:[UIColor cyanColor]];
    UILabel *label = [[UILabel alloc] init];
    label.font = [UIFont systemFontOfSize:14];
    label.textColor = [UIColor darkGrayColor];
    label.text = [[_viewControllers objectAtIndex:section] objectForKey:@"title"];
    label.numberOfLines = 1;
    [label sizeToFit];
    CGRect frame = label.frame;
    frame.origin.x = 15;
    frame.origin.y = header.bounds.size.height - frame.size.height;
    label.frame = frame;
    [header addSubview:label];
    return header;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 0.01;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSDictionary * sections = _viewControllers[indexPath.section];
    
    NSArray * items = [sections objectForKey:@"items"];
    
    NSString *clsStr  = [items[indexPath.row] objectForKey:@"class"];
    
    id vc = [[NSClassFromString(clsStr) alloc] init];
    
    [self.navigationController pushViewController:vc animated:YES];
}
@end
